package com.sc.auction.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sc.auction.common.pojo.Auction;
import com.sc.auction.goods.feign.GoodsAuctionService;
import com.sc.auction.goods.feign.GoodsOssService;
import com.sc.auction.goods.pojo.Goods;
import com.sc.auction.goods.pojo.Goodstype;
import com.sc.auction.goods.pojo.Images;
import com.sc.auction.goods.service.GoodsService;
import com.sc.auction.common.exception.AuctionException;
import com.sc.auction.common.util.R;
import com.sc.auction.goods.service.GoodstypeService;
import com.sc.auction.goods.service.ImagesService;
import com.sc.auction.goods.vo.GoodsBean;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.auction.goods.vo.GoodsQuery;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lsc
 * @since 2020-08-31
 */
@CrossOrigin
@RestController
@RequestMapping("/auction/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodstypeService goodstypeService;
    @Autowired
    private ImagesService imagesService;

    @Autowired
    private GoodsAuctionService goodsAuctionService;
    @Autowired
    private GoodsOssService goodsOssService;

    @ApiOperation("查询所有商品")
    @GetMapping("/goodsListPage")
    public R goodsListPage(@ApiParam(name = "page",defaultValue = "1",required = true)@RequestParam("page") Integer page,
                           @ApiParam(name = "limit",defaultValue = "5",required = true)@RequestParam("limit") Integer limit,
                           GoodsQuery goodsQuery) throws AuctionException {

        Page<Goods> pageBean=new Page<>(page,limit);
        if (goodsQuery!=null){
            QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
            if (goodsQuery.getName()!=null&&goodsQuery.getName()!=""){
                queryWrapper.like("name","%"+goodsQuery.getName()+"%");
            }
            if (goodsQuery.getType()!=null&&goodsQuery.getType()>0){
                queryWrapper.eq("type",goodsQuery.getType());
            }
            goodsService.page(pageBean, queryWrapper);
        }
        List<Goods> goodsList = pageBean.getRecords();
        List<GoodsBean>  records=new ArrayList<>();
        for (Goods goods:goodsList) {
            QueryWrapper<Goodstype> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("id",goods.getType());
            Goodstype goodstype = goodstypeService.getOne(queryWrapper);
            QueryWrapper<Images> imagesQueryWrapper=new QueryWrapper<>();
            imagesQueryWrapper.eq("goodsCode",goods.getGoodsCode());
            Images images = imagesService.getOne(imagesQueryWrapper);
            GoodsBean goodsBean=new GoodsBean();
            goodsBean.setGoods(goods);
            goodsBean.setGoodstype(goodstype.getType());
            goodsBean.setUrl(images.getUrl());
            records.add(goodsBean);
        }
        long total = pageBean.getTotal();

        if (records!=null&&records.size()>0){
            return R.ok().data("total",total).data("records",records);
        }else {
            throw  new AuctionException(201,"未查找到拍品信息");
        }
    }

    @ApiOperation("/增加商品")
    @PostMapping ("/addGoods")
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public R addGoods(@RequestBody GoodsBean goodsBean) throws AuctionException {
        System.out.println(goodsBean+"wojowajofjhgehefj00000000000000000000000");
        Goods goods = goodsBean.getGoods();
        String url=goodsBean.getUrl();
        long time = new Date().getTime();
        String goodsCode = time + "";
        goods.setGoodsCode(goodsCode);
        Images images=new Images();
        images.setGoodsCode(goodsCode);
        images.setUrl(url);
        System.out.println("goods+********"+goods);
        boolean saveImg = imagesService.save(images);
        boolean save = goodsService.save(goods);
        if (save&&saveImg){
            return R.ok();
        }else {
            throw new AuctionException(201,"增加失败");
        }
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/deleteGoods")
    public R  deleteGoods(Integer id) throws AuctionException {
        if (id!=null&&id>0){
            boolean b = goodsService.removeById(id);
            if (b){
                return R.ok();
            }else {
                throw  new AuctionException(201,"删除失败");
            }
        }
         throw new AuctionException(201,"错误操作");
    }
    @ApiOperation("修改商品")
    @PostMapping("/editGoods")
    public R  editGoods(@RequestBody GoodsBean goodsBean) throws AuctionException {
        Goods goods = goodsBean.getGoods();
        String url=goodsBean.getUrl();
        QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",goods.getId());
        boolean update = goodsService.update(goods, queryWrapper);
        Goods g=goodsService.getOne(queryWrapper);
        QueryWrapper<Images> queryWrapperImg=new QueryWrapper<>();
        System.out.println(goods.getGoodsCode()+"wowowoowwoow"+g.getGoodsCode());
        queryWrapperImg.eq("goodsCode",g.getGoodsCode());
        imagesService.remove(queryWrapperImg);
        Images images=new Images();
        images.setGoodsCode(g.getGoodsCode());
        images.setUrl(url);
        boolean saveImg = imagesService.save(images);
        if (update&&saveImg){
            return R.ok();
        }else {
            throw new AuctionException(201,"修改失败");
        }
    }
    @ApiOperation("根据id查询商品")
    @GetMapping("/selectGoodsById")
    public R selectGoodsById(@RequestParam("id") Integer id) throws AuctionException {

        Goods goods = goodsService.getById(id);
        if (goods!=null){
            return R.ok().data("goods",goods);
        }else {
            throw new AuctionException(201,"查询失败");
        }
    }
    @ApiOperation("发布商品")
    @PostMapping("/issueGoods")
    public R   issueGoods(@RequestBody Auction auction) throws AuctionException {
        R r = goodsAuctionService.postAuction(auction);
        Integer code = r.getCode();
        QueryWrapper<Goods> queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",auction.getGoodsId());
        Goods goods=new Goods();
        goods.setStatus(2);
        boolean update = goodsService.update(goods, queryWrapper);
        if (code==200&&update){
            return R.ok().message("发布成功");
        }else {
            throw new AuctionException(201,"发布失败");
        }
    }
    @ApiOperation("流拍商品再次发布发布")
    @GetMapping("issueGoodsAgain")
    public R   issueGoodsAgain(){
        return null;
    }
    @ApiOperation("测试图片")
    @PostMapping("/test")
    public R  img(@ApiParam(value = "文件",required = true) @RequestParam(value = "file") MultipartFile file,
                  @ApiParam(value = "模块",required = true) @RequestParam(value = "module") String module){
        R upload = goodsOssService.upload(file, module);
        Object url = upload.getData().get("url");
        if (url!=null){
            System.out.println(url+"************这是图片url");
            return R.ok();
        }else {
            return R.error();
        }

    }
    public void sss(){

    }

    @ApiOperation("根据id查询img")
    @GetMapping("/getImgById")
    public R getImgById(@RequestParam("aid") Integer aid) throws AuctionException {
        Goods goods = goodsService.getById(aid);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("goodsCODE",goods.getGoodsCode());
        Images images=imagesService.getOne(queryWrapper);
        return R.ok().data("imageURL",images.getUrl());
    }
    @ApiOperation("测试gateway")
    @GetMapping("SUOYOU")
    public R   get(){
        List<Goods> list = goodsService.list();
        return R.ok().data("list",list);
    }
}

